/**
 * @file 工单列表
 * @author yangjiangming <449654337@qq.com>
 * @date 2018/12/03 11:05:40
 */
layui
  .extend({
    oms_work_config: "work/config/zmn.oms.work.config",
    mos_order_add_common: "order/zmn.mos.order.add.common",
    oms_work_modify_route: 'work/modify/common/zmn.modify.route'
  })
  .define(
    [
      "oms_work_config",
      "mos_order_add_common",
      "zmnUtils",
      "layer",
      "form",
      "$http",
      "table",
      'oms_work_modify_route'
    ],
    function (exports) {
      var addUtils = layui.mos_order_add_common;
      var config = layui.oms_work_config;
      var zmnUtils = layui.zmnUtils;
      var $http = layui.$http;
      var $ = layui.$;
      var route = layui.oms_work_modify_route;

      // 下单统计 URL
      var ORDER_COUNT_URL = "/order/getOrderCountByTelephone";

      // 设置下单统计
      var initOrderCount = (function () {
        /**
         * 设置下单统计
         * @param count 数量
         */
        function setOrderCount(count) {
          this.text(count);
        }

        /**
         * 远程根据电话号码获取下单统计
         * @param telephone 电话号码
         * @return 结果
         */
        function fetchOrderCountByTelephone(telephone) {
          return $http.get(ORDER_COUNT_URL, { telephone: telephone });
        }

        return function (telephone, $countWrapper) {
          if (!telephone) {
            setOrderCount.call($countWrapper, "");
            return;
          }

          fetchOrderCountByTelephone(telephone).then(
            function (count) {
              setOrderCount.call($countWrapper, count);
            },
            function () {
              setOrderCount.call($countWrapper, "");
            }
          );
        };
      })();

      /**
       * 初始化页面
       */
      function initPage() {
        addUtils.init({
          isNew: true,
          enableExternalShop: true,
          isDistributeMaster: true,
          saveURL: config.url.saveSimpleURL,
          success: function () {
            location.reload();
          },
        });
      }

      /**
       * 初始化页面事件
       */
      function initEvent() {

        //输入框正在输入时
        $("input[name='channelPrepayAmount']").on('input',function(){
          var $this = $(this);
          var value = $this.val()
              .replace(/\-{1,}/g,'')
              .replace(/[^\d.-]/g, '')
              .replace(/^\./g, '')
              .replace(/\.{2,}/g, '.')
              .replace('.', '$#$').replace(/\./g, '').replace('$#$', '.')
              .replace(/^(\d+)\.(\d\d).*$/, '$1.$2');
          $this.val(value);
        });

        // 物流信息
        // addUtils.renderNewOrderPost();

        // 监听电话号码输入框 change 时间
        $("#telephone, #telephone2").on("change", function (e) {
          var $input = $(this);
          var telephone = addUtils.getters.getTelephoneById($input.attr('id'));
          var $count = $input.parent("div").find(".order-count");

          initOrderCount(telephone, $count);
        });

        // 话务列表页面
        $('#order_count_tel1').on('click', function () {
          let orderCount = $(this).text();
          if (orderCount == null || orderCount == '' || orderCount == '0') {
            return false;
          }
          let $telephone = $('#telephone');
          let phone = $telephone.val();
          let cryptoPhone = $telephone.data('crypto-phone');
          parent.layui.index.openTabsPage(route._calculateHostForOMS() + "/customerservice/work/list.action?concatPhone=" + phone + "&cryptoPhone=" + cryptoPhone, '话务工单');
          return false;
        });
      }

      /**
       * 初始化表单数据
       */
      function initFormData() {
        var tel = zmnUtils.getQueryString("telephone");
        var channelId = zmnUtils.getQueryString("channelId");
        var cityId = zmnUtils.getQueryString("cityId");

        if (tel) {
          $("#telephone").val(tel);
        }

        if (channelId) {
          var select = "dd[lay-value=" + channelId + "]";
          $("#channelId")
            .siblings("div.layui-form-select")
            .find("dl")
            .find(select)
            .click();
        }

        if (cityId) {
          var select = "dd[lay-value=" + cityId + "]";
          $("#cityId")
            .siblings("div.layui-form-select")
            .find("dl")
            .find(select)
            .click();
        }
      }

      function init() {
        // 初始化表单数据
        initFormData();

        // 初始化页面
        initPage();

        // 初始化事件
        initEvent();
      }

      init();
      exports("oms_order_simpleAdd");
    }
  );
